using System.Collections.Generic;
using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;

namespace AndyPike.Unfuddle.Commands.Tickets
{
    /// <summary>
    /// Provides a command for returning all tickets for a given project
    /// </summary>
    public class FindAllTicketsForProject : IUnfuddleFindCommand<List<Ticket>>
    {
        private readonly int projectId;

        /// <summary>
        /// Instantiates the FindAllTicketsForProject command
        /// </summary>
        /// <param name="projectId">The id of the project to filter tickets by</param>
        public FindAllTicketsForProject(int projectId)
        {
            this.projectId = projectId;
        }

        /// <summary>
        /// Queries the Unfuddle server for all tickets for a given project
        /// </summary>
        /// <param name="service">The Unfuddle service to use when executing the command</param>
        /// <returns>Returns a list of projects for the given account</returns>
        public List<Ticket> Execute(IUnfuddleService service)
        {
            GuardAgainst.ArgumentNull(service, "service");

            string url = service.BuildUrl("projects/{0}/tickets", projectId);
            var collection = service.InvokeRequest<TicketsCollection>(url);
            return collection.Tickets;
        }
    }
}